package com.hobart.learning.lagou.edu.utils;

import com.hobart.learning.lagou.edu.annotation.Autowired;
import com.hobart.learning.lagou.edu.annotation.Component;

import java.sql.SQLException;

/**
 * @author: Hobart Mr.Bai
 * @create: 2021-02-03 09:37
 * @description: 事务控制
 * @Version 1.0
 **/
@Component
public class TransactionManager {

//    private TransactionManager() {
//    }
//
//    private static TransactionManager transactionManager = new TransactionManager();
//
//    public static TransactionManager getInstance() {
//        return transactionManager;
//    }

    @Autowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    // 开启事务
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    // 提交事务
    public void commitTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }

    // 回滚事务
    public void rollbackTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
